package com.tt.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tt.reggie.common.exception.CustomException;
import com.tt.reggie.entity.Category;
import com.tt.reggie.entity.Dish;
import com.tt.reggie.entity.Employee;
import com.tt.reggie.entity.Setmeal;
import com.tt.reggie.mapper.CategoryMapper;
import com.tt.reggie.mapper.EmployeeMapper;
import com.tt.reggie.service.CategoryService;
import com.tt.reggie.service.DishService;
import com.tt.reggie.service.EmployeeService;
import com.tt.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类，并判断当前产品是否关联菜品或套餐
     * @param id
     */
    @Override
    public boolean remove(long id) {
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<Dish>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(lambdaQueryWrapper);

        if(count>0){
            //说明当前分类关联着菜品
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(lambdaQueryWrapper1);

        if(count1>0){
            //说明当前分类关联着套餐
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        //正常删除分类
        return removeById(id);

    }
}
